<?php

class GbookController extends Controller
{


	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view', 'create'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}


  
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
	   $this->Defaultmetatag;
      $this->importmodel();
       $this->setlayer();
       
		$model=new Gbook;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Gbook']))
		{
			$model->attributes=$_POST['Gbook'];
			if($model->save())
				$this->redirect(array('index'));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}





	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
	   $this->Defaultmetatag;
       
	   $this->importmodel();
       $this->setlayer();  
       
       $criteria = new CDbCriteria;
       $criteria->select ='login, message, answer, date, status';
       $criteria->order = 'id DESC';
       
       $count=Gbook::model()->count($criteria);              //Посчитать количество записей
       $pages=new CPagination($count);
         
       $pages->pageSize=3;                                   // количество элементов на страницу
       $pages->applyLimit($criteria);
       
       $model = Gbook::model()->findAll($criteria);
              $this->render('index', array('model'=>$model, 
                                           'pages'=>$pages
                                           ));
            
	}


	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Gbook::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='gbook-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
    
    
    
     private function importmodel()
    {
        
         Yii::import('application.modules.admin.modules.gbook.models.*');    //Подключение моделей из модуля блога
    }
    

    private function setlayer()
    {
      $this->layout = Linkmenu::setlayout(31);                               //Устанавливает слой для данного контроллера по url из таблицы Linkmenu  
    }
    
    
    
    
    
    
}
